package com.matrix.java.chap_06.example;

public class Case6_07 {
    public static void main(String[] args) {
        System.out.println("The first 50 prime numbers are");
        printPrimeNumbers(50);
    }

    private static void printPrimeNumbers(int numberOfPrimes) {
        final int NUMBER_OF_PRIMES_PER_LINE = 10;
        int count = 0;
        int number = 2;
        while (count < numberOfPrimes) {
            if (isPrime(number)){
                count++;// Increase the count
                if (count%NUMBER_OF_PRIMES_PER_LINE==0){
                    System.out.printf("%-5s%n",number);
                }else {
                    System.out.printf("%-5s",number);
                }
            }
            // Check whether the next number is prime
            number++;
        }
    }

    private static boolean isPrime(int number) {
        for (int divisor = 2; divisor <= number / 2; divisor++) {
            if (number % divisor == 0) { // If true, number is not prime
                return false;// Number is not a prime
            }
        }
        return true;// Number is prime
    }
}
